package com.g7.figmaapi.web.api.listener;

import com.alibaba.fastjson.JSONObject;
import com.g7.figmaapi.common.utils.RedisUtils;
import com.g7.figmaapi.web.api.figma.FigmaConstant;
import com.g7.figmaapi.web.api.listener.event.WebApiCallBackEvent;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @author: taoxiangqian
 * @explain: 三方API监听器
 * @since: 2023/10/30 09:11
 */
@Slf4j
@Component
public class WebApiCallBackListener {

    @Async
    @TransactionalEventListener(fallbackExecution = true, classes = {WebApiCallBackEvent.class})
    public void webApiListener(WebApiCallBackEvent webApiEvent) {
        log.info("三方API监听器, 参数信息:{}", JSONObject.toJSONString(webApiEvent));
        if (WebApiCallBackEvent.EVENT_TYPE_CLEAR_REDIS.equals(webApiEvent.getEventType())) {
            handleClearRedisEvent(webApiEvent.getChannelType(), webApiEvent.getKey());
        }
    }

    /**
     * 处理清除redis事件
     *
     * @param key redis key
     * @return void
     * @author taoxiangqian
     * @since 2023/11/15
     **/
    private void handleClearRedisEvent(String channelType, String key) {
        log.info("三方回调清除redis事件,渠道类型{}，key{}", channelType, key);
        RedisUtils.del(key);
    }
}
